
if (!Array.indexOf) {
    Array.prototype.indexOf = function (obj) {
        var len = this.length;
        for (var i = 0; i < len; i++) {
            if (this[i] === obj) {
                return i;
            }
        }
        return -1;
    };
}
//字符编码数值对应的存储长度：
//UCS-2编码(16进制) UTF-8 字节流(二进制) 
//0000 - 007F 0xxxxxxx （1字节）
//0080 - 07FF 110xxxxx 10xxxxxx （2字节）
//0800 - FFFF 1110xxxx 10xxxxxx 10xxxxxx （3字节） 
String.prototype.getBytesLength = function () {
    var totalLength = 0;
    var charCode;
    for (var i = 0; i < this.length; i++) {
        charCode = this.charCodeAt(i);
        if (charCode <= 0x007f) {
            totalLength++;
        } else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
            totalLength += 2;
        } else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
            totalLength += 3;
        } else {
            totalLength += 4;
        }
    }
    return totalLength;
};

String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

String.prototype.changeHtmlChar = function () {
    var rt = this.replace(/&/g, "&amp;");
    rt = rt.replace(/</g, "&lt;");
    rt = rt.replace(/>/g, "&gt;");
    rt = rt.replace(/\s/g, "&nbsp;");
    return rt;
};

String.prototype.resumeHtmlChar = function () {
    var rt = this.replace(/&amp;/g, "&");
    rt = rt.replace(/&lt;/g, "<");
    rt = rt.replace(/&gt;/g, ">");
    rt = rt.replace(/&quot;/g, "\"");
    rt = rt.replace(/&apos;/g, "'");
    rt = rt.replace(/&nbsp;/g, " ");
    return rt;
};

String.prototype.isEmail = function () {
    var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (reg.test(this)) {
        return true;
    } else {
        return false;
    }
};

function myEscape(str) {
    var rt = escape(str);//此函数没有处理+号/号，空格转为%20而不会转为+号
    rt = rt.replace(/\+/g, "%2B");
    rt = rt.replace(/\//g, "%2F");
    return rt;
}

function wdCancelBubble(ev) {
    if (!ev)
        ev = window.event;
    if (ev.stopPropagation)
        ev.stopPropagation();
    else
        ev.cancelBubble = true;
}

function wdEle(id) {
    return document.getElementById(id);
}

function wdDocWidth() {
    return document.documentElement.clientWidth;
}

function wdDocHeight() {
    return document.documentElement.clientHeight;
}

function wdLoadScript(url, id) {
    var item = document.getElementById("script_" + id);
    if (item) {
        return null;//已有相同script，返回
    }

    var script = document.createElement("script");
    script.id = "script_" + id;
    script.type = "text/javascript";
    script.src = url;

    var head = document.getElementsByTagName("head")[0];
    head.appendChild(script);

    return script;
}

function wdLoadCss(url, id) {
    var item = document.getElementById("link_" + id);
    if (item) {
        return null;//已有相同css，返回
    }

    var link = document.createElement("link");
    link.id = "link_" + id;
    link.type = "text/css";
    link.rel = "stylesheet";
    link.href = url;

    var head = document.getElementsByTagName("head")[0];
    head.appendChild(link);

    return link;
}

function wdBrowserType() {
    var ua = navigator.userAgent.toLowerCase();
    return (ua.indexOf("applewebkit") > 0 ? 1 : (ua.indexOf("firefox") > 0 ? 2 : (ua.indexOf("msie") > 0 ? 3 : 0)));
}

function object2Json(obj) {
    var json = null;

    if (obj instanceof Array) {
        json = "[";

        for (var i = 0; i < obj.length; i++) {
            var member = obj[i];

            if (typeof (member) === "function") {//是方法
            } else if (member === null) {
                json += "null,";
            } else {//是属性
                if (member instanceof Array) {//是数组
                    json += object2Json(member) + ",";
                } else if (typeof (member) === "object") {//是对象
                    json += object2Json(member) + ",";
                } else if (typeof (member) === "string") {//是字符串
                    json += "\"" + member + "\",";
                } else if (typeof (member) === "boolean") {//是布尔
                    json += (member ? "true" : "false") + ",";
                } else {//是其他类型
                    json += member + ",";
                }
            }
        }
        json = json.replace(/,*$/, "");
        json += "]";
    } else if (typeof (obj) === "object") {
        json = "{";

        for (var m in obj) {
            var member = obj[m];

            if (typeof (member) === "function") {//是方法
            } else if (member === null) {
                json += "\"" + m + "\":null,";
            } else {//是属性
                if (member instanceof Array) {//是数组
                    json += "\"" + m + "\":" + object2Json(member) + ",";
                } else if (typeof (member) === "object") {//是对象
                    json += "\"" + m + "\":" + object2Json(member) + ",";
                } else if (typeof (member) === "string") {//是字符串
                    json += "\"" + m + "\":\"" + member + "\",";
                } else if (typeof (member) === "boolean") {//是布尔
                    json += "\"" + m + "\":" + (member ? "true" : "false") + ",";
                } else {//是其他类型
                    json += "\"" + m + "\":" + member + ",";
                }
            }
        }
        json = json.replace(/,*$/, "");
        json += "}";
    }

    return json;
}

//异步请求wdRequest已经转移到独立的JS文件

function alertMsg(msg, state, msg_div) {
    msg_div = arguments[2] ? arguments[2] : "message";
    var div = document.getElementById(msg_div);
    div.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;" + msg;
    if (state===true) {
        div.className = "red_tips";
    } else if (state===false){
        div.className = "tips";
    }else if(state==="succeed"){
          div.className = "green_tips";
    }
}
var priv;
function checkRolePriv() {
    if (parseInt(priv) === 1) { // 可见
//        $(".priv_act").attr("disabled",true);
    }
}

if (window.addEventListener) {
    window.addEventListener("load", load, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", load);
}

function load() {
    checkRolePriv();
}
